package com.suning.sawp.web.controller;

import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.nsf.istore.coffee.po.CoffeeOrderRecord;
import com.suning.nsf.istore.coffee.service.intf.RemoteCoffeeService;
import com.suning.nsfsrv.trade.coffee.CoffeeTradeServer;
import com.suning.nsfsrv.trade.coffee.dto.OrderInfoQry;
import com.suning.nsfsrv.trade.coffee.dto.OrderInfoResult;
import com.suning.nsfsrv.trade.coffee.dto.OrderItemOperRet;
import com.suning.rsf.consumer.ServiceLocator;
import com.suning.sawp.dto.storeman.DateEditor;
import com.suning.sawp.dto.storeman.JsonResult;
import com.suning.sawp.service.util.GsonUtils;

/**
 * 〈云店验单功能〉<br>
 * 〈包括c店和自营〉
 *
 * @author 12061748
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
@RequestMapping("storeMan")
public class CoffeeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CoffeeController.class);
    @Autowired
    RemoteCoffeeService remoteCoffeeServiceClient;

    private CoffeeTradeServer coffeeTradeServer = ServiceLocator.getService(CoffeeTradeServer.class,
            "NSFRSV-CoffeeTradeServer");

    /**
     * 
     * 功能描述: c店根据门店编码查询门店活动列表<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @ResponseBody
    @RequestMapping(value = "storeIsHasActivities.do", produces = "application/json; charset=utf-8")
    public JsonResult storeIsHasActivities(@RequestParam String storeCode) {
        Map<String, Object> data = remoteCoffeeServiceClient.getStoreActivities(storeCode);
        return new JsonResult(data);
    }

    /**
     * 
     * 功能描述: c店通过用户提供的消费码查询咖啡订单信息<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @ResponseBody
    @RequestMapping(value = "queryCoffeeOrderInfo.do", produces = "application/json; charset=utf-8")
    public JsonResult queryCoffeeOrderInfo(@RequestParam String sellerCode, @RequestParam String orderCode) {
        Map<String, Object> data = remoteCoffeeServiceClient.queryCoffeeOrderInfo(sellerCode, orderCode);
        return new JsonResult(data);
    }

    /**
     * 
     * 功能描述: c店确认领取咖啡<br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @ResponseBody
    @RequestMapping(value = "confirmCoffeeOrder.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public JsonResult confirmCoffeeOrder(CoffeeOrderRecord coffeeOrderRecord) {
        String data = remoteCoffeeServiceClient.confirmCoffeeOrder(coffeeOrderRecord);
        if ("1".equals(data)) {
            return new JsonResult("");
        }
        return new JsonResult("", "订单异常");
    }

    /**
     * 
     * 功能描述: 自营订单查询<br>
     * 〈功能详细描述〉
     *
     * @param type
     * @param orderInfoQry
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @ResponseBody
    @RequestMapping(value = "/checkOrder/query_{type}.do")
    public String orderQuery(@PathVariable int type, OrderInfoQry orderInfoQry) {
        OrderInfoResult result = new OrderInfoResult();
        try {
            switch (type) {
                case QueryType.QUERY_PS:
                    result = coffeeTradeServer.queryNoAcceptDeliveryOrders(orderInfoQry);
                    break;
                case QueryType.QUERY_YJD:
                    result = coffeeTradeServer.queryNoSendDeliveryOrders(orderInfoQry);
                    break;
                case QueryType.QUERY_ZT:
                    result = coffeeTradeServer.querySelfGetOrders(orderInfoQry);
                    break;
                case QueryType.QUERY_YWC:
                    result = coffeeTradeServer.queryHasSendOrders(orderInfoQry);
                    break;
                default:
                    result = new OrderInfoResult();
                    break;
            }
        } catch (Exception e) {
            LOGGER.error("调用rsf获取云店验单信息异常", e);
            result.setRetFlag(1);
            result.setErrCode("1001");
            result.setMessage("系统异常");
        }
        return GsonUtils.toJson(result, false);
    }

    /**
     * 
     * 功能描述: 自营订单操作（接单，配送，确认送达）<br>
     * 〈功能详细描述〉
     *
     * @param type
     * @param orderInfoQry
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @ResponseBody
    @RequestMapping(value = "/checkOrder/handle_{type}.do")
    public String orderHandle(@PathVariable int type, String b2cOrderId, String orderId, String employeeNo,
            String employeeName) {
        OrderItemOperRet result = new OrderItemOperRet();
        try {
            switch (type) {
                case HandleType.HANDLE_JD:
                    result = coffeeTradeServer.acceptDeliveryOrder(b2cOrderId, orderId, employeeNo, employeeName);
                    break;
                case HandleType.HANDLE_C_FH:
                    result = coffeeTradeServer.sendDeliveryOrder(b2cOrderId, orderId, employeeNo, employeeName);
                    break;
                case HandleType.QUERY_ZY_FH:
                    result = coffeeTradeServer.sendSelfGetOrder(b2cOrderId, orderId, employeeNo, employeeName);
                    break;
                default:
                    result = new OrderItemOperRet();
                    break;
            }
        } catch (Exception e) {
            LOGGER.error("调用rsf操作云店验单信息异常", e);
            result.setRetFlag(1);
            result.setErrCode("1001");
            result.setMessage("系统异常");
        }
        return GsonUtils.toJson(result, false);
    }

    /**
     * 
     * 〈自营验单查询类型〉<br>
     * 〈功能详细描述〉
     *
     * @author 12061748
     * @see [相关类/方法]（可选）
     * @since [产品/模块版本] （可选）
     */
    interface QueryType {
        /**
         * 自营配送
         */
        int QUERY_PS = 1;
        /**
         * 自营已接单
         */
        int QUERY_YJD = 2;
        /**
         * 自营自提
         */
        int QUERY_ZT = 3;
        /**
         * 自营已完成
         */
        int QUERY_YWC = 4;
    }

    /**
     * 
     * 〈自营验单处理类型〉<br>
     * 〈功能详细描述〉
     *
     * @author 12061748
     * @see [相关类/方法]（可选）
     * @since [产品/模块版本] （可选）
     */
    interface HandleType {
        /**
         * 自配送订单接单操作
         */
        int HANDLE_JD = 1;
        /**
         * 配送订单发货操作
         */
        int HANDLE_C_FH = 2;
        /**
         * 自提订单发货操作
         */
        int QUERY_ZY_FH = 3;
    }

    @InitBinder
    protected void initBinder(ServletRequestDataBinder binder) {
        // 对于需要转换为Date类型的属性，使用DateEditor进行处理
        binder.registerCustomEditor(Date.class, new DateEditor());
    }
}
